using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System;
using System.Collections.Generic;

namespace IQIGame.Onigao.GamePlay
{
    public class UD_EquipInfo : UIBaseData
    {
        public UIEquipInfoTabType viewType { get; set; } = UIEquipInfoTabType.Details;
        public ItemEquipmentData equipItem { get; private set; }
        private List<ItemEquipmentData> _equipItems = new(8);
        public IReadOnlyList<ItemEquipmentData> equipItems => this._equipItems;
        public int equipCount => this._equipItems.Count;
        public int equipIndex => this.IndexOf(this.equipItem);

        private IReadOnlyDictionary<int, int> _dicAllAttributeCid2Value = null;
        private List<int> _allAttributeCids = new(8);
        public IReadOnlyList<int> allAttributeCids => this._allAttributeCids;

        public UD_EquipInfo Sync(UIEquipInfoTabType viewType, ItemEquipmentData equipItem, List<ItemEquipmentData> equipItems)
        {
            this.viewType = viewType;
            this.equipItem = equipItem;
            this._equipItems.Clear();
            if (equipItems != null && equipItems.Count > 0)
            {
                this._equipItems.AddRange(equipItems);
            }
            else
            {
                this._equipItems.Add(equipItem);
            }

            this._dicAllAttributeCid2Value = AttributeDataUtils.GetEquipAllAttributeDic(this.equipItem, GameDataCenter.PlayerData.equipSkins);
            this._allAttributeCids.Clear();
            this._allAttributeCids.AddRange(this._dicAllAttributeCid2Value.Keys);
            return this;
        }

        public void ChangeEquip(ItemEquipmentData equipItem)
        {
            this.equipItem = equipItem;
        }

        public void ChangeEquip(int index)
        {
            this.equipItem = this.equipItems[index];
        }

        public int IndexOf(ItemEquipmentData equipItem)
        {
            return this.equipItems.IndexOf(equipItem);
        }

        public ItemEquipmentData GetEquip(int index)
        {
            return this.equipItems[index];
        }

        public void RefresEquipItems()
        {
            var itemModuleData = GameDataCenter.PlayerData.item;
            for (int i = this._equipItems.Count - 1; i >= 0; i--)
            {
                ItemEquipmentData equipItem = this._equipItems[i];
                if (equipItem.id <= 0 || !itemModuleData.IsContainsItem(equipItem.id))
                {
                    this._equipItems.Remove(equipItem);
                }
            }
        }

        public int GetAttributeValue(int attributeCid)
        {
            return this._dicAllAttributeCid2Value.TryGetValue(attributeCid, out var value) ? value : 0;
        }

        public int GetLevelAttributeGrowth(int attributeCid)
        {
            return AttributeDataUtils.GetEquipLevelAttributeGrowth(this.equipItem, attributeCid);
        }

        public void Clear()
        {
            this.viewType = UIEquipInfoTabType.Details;
            this.equipItem = null;
            this._equipItems.Clear();
            this._dicAllAttributeCid2Value = null;
            this._allAttributeCids.Clear();
        }

        #region 对象池

        public override void OnReset()
        {
            this.Clear();
        }

        static public UD_EquipInfo NewFromPool()
        {
            return ClassPool<UD_EquipInfo>.Get();
        }
        #endregion

    }
}
